package com.lazy.utils.axis;

import com.lazy.utils.common.FastJsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.apache.commons.lang3.StringUtils;

import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 用于调用 webservice接口服务
 * @author zk
 * @date 2018-05-24 11:26
 */
@Slf4j
public class AxisUtil {

    /**
     * 调取 webservice 服务方法
     *
     * @param webServiceDTO webservice 参数
     * @return 接口结果
     */
    private static String getService(WebServiceDTO webServiceDTO) throws Exception {
        log.info("AxisUtil#getService start webServiceDTO-{}", FastJsonUtil.obj2json(webServiceDTO));
        String endpoint = webServiceDTO.getEndpoint();
        String targetNamespace = webServiceDTO.getTargetNamespace();
        String method = webServiceDTO.getMethod();
        Map<String, Object> params = webServiceDTO.getParams();
        String soapActionURL = webServiceDTO.getSoapActionURI();
        try {
            Service service = new Service();
            Call call = (Call) service.createCall();
            call.setTargetEndpointAddress(new java.net.URL(endpoint));
            if(StringUtils.isNotBlank(targetNamespace)){
                call.setOperationName(new QName(targetNamespace,method));
            }else{
                call.setOperationName(method);
            }
            call.setUseSOAPAction(true);
            call.setSOAPActionURI(soapActionURL);
            Object[] objects = new Object[params.keySet().size()];
            int i = 0;
            for (Map.Entry<String, Object> entry : params.entrySet()) {
                Object value = entry.getValue();
                if (value instanceof Integer) {
                    if(StringUtils.isNotBlank(targetNamespace)){
                        call.addParameter(new QName(targetNamespace, entry.getKey()), XMLType.XSD_INTEGER,ParameterMode.IN);
                    }else{
                        call.addParameter(entry.getKey(), XMLType.XSD_INTEGER, Integer.class, ParameterMode.IN);
                    }
                } else {
                    if(StringUtils.isNotBlank(targetNamespace)){
                        call.addParameter(new QName(targetNamespace, entry.getKey()), XMLType.XSD_STRING,ParameterMode.IN);
                    }else{
                        call.addParameter(entry.getKey(), XMLType.XSD_STRING, String.class, ParameterMode.IN);
                    }
                }
                objects[i] = FastJsonUtil.obj2json(value);
                i++;
            }
            call.setReturnType(XMLType.XSD_STRING);
            String result = (String) call.invoke(objects);
            log.info("AxisUtil#getService start webServiceDTO-{},result-{}", FastJsonUtil.obj2json(webServiceDTO),result);
            return result;
        } catch (Exception e) {
            log.error("AxisUtil#getService fail webServiceDTO-{},e-{}", FastJsonUtil.obj2json(webServiceDTO), e);
            throw e;
        }
    }

    public static void main(String[] args) {
        WebServiceDTO webServiceDTO = new WebServiceDTO();
        webServiceDTO.setEndpoint("http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx?wsdl");
        webServiceDTO.setTargetNamespace("http://WebXml.com.cn/");
        webServiceDTO.setMethod("qqCheckOnline");
        webServiceDTO.setSoapActionURI("http://WebXml.com.cn/qqCheckOnline");

        Map<String, Object> params = new LinkedHashMap();
        params.put("qqCode", "673820543");
        webServiceDTO.setParams(params);

        String result = null;
        try {
            result = getService(webServiceDTO);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(result);
    }


}
